<?php
class LyLinkPager extends CLinkPager {
	
	public $cssFile = false;
	public $header = '';
	public $firstPageLabel = '首页';
	public $lastPageLabel = '尾页';
	public $nextPageLabel = '下一页';
	public $prevPageLabel = '<';
	
	public $hasFirst = true;
	public $hasPrev = true;
	public $hasNext = true;
	public $hasLast = true;
	public $hasInternal = true;
	
	public $htmlOptions = array(
		'class' => '',
	);
	
	/**
	 * Creates the page buttons.
	 * @return array a list of page buttons (in HTML code).
	 */
	protected function createPageButtons()
	{
		if(($pageCount=$this->getPageCount())<=1)
			return array();

		list($beginPage,$endPage)=$this->getPageRange();
		$currentPage=$this->getCurrentPage(false); // currentPage is calculated in getPageRange()
		$buttons=array();

		// first page
//		if($this->hasFirst)
//			$buttons[]=$this->createPageButton($this->firstPageLabel,0,self::CSS_FIRST_PAGE,$currentPage<=0,false);

		// prev page
		if(($page=$currentPage-1)<0)
			$page=0;
		if($this->hasPrev && $currentPage !=0)
		$buttons[]=$this->createPageButton($this->prevPageLabel,$page,self::CSS_PREVIOUS_PAGE,$currentPage<=0,false);

		// internal pages
		if($this->hasInternal)
			for($i=$beginPage;$i<=$endPage;++$i)
				$buttons[]=$this->createPageButton($i+1,$i,self::CSS_INTERNAL_PAGE,false,$i==$currentPage);

		// next page
		if(($page=$currentPage+1)>=$pageCount-1)
			$page=$pageCount-1;
		if($this->hasNext && $currentPage != $this->getPageCount()-1)
		$buttons[]=$this->createPageButton($this->nextPageLabel,$page,'bh-c-xiayiye1-yy',$currentPage>=$pageCount-1,false); 
		
		// last page
//		if($this->hasLast)
//			$buttons[]=$this->createPageButton($this->lastPageLabel,$pageCount-1,self::CSS_LAST_PAGE,$currentPage>=$pageCount-1,false);
		return $buttons;
	}
	
	public function run() {
		$form=$this->beginWidget('CActiveForm',array(
			'enableAjaxValidation'=>false,
//			'action' => Yii::app()->createUrl( $this->getController()->route),
			'method' => 'get'
		));
		$this->registerClientScript();
		$buttons=$this->createPageButtons();
		if(empty($buttons))
			return;
		foreach ($buttons as $one) {
			echo $one;
		}
		$pageObj = $this->getPages();
		$page = isset($_GET[$pageObj->pageVar]) ? $_GET[$pageObj->pageVar] : 1;
		echo '到第';
		echo CHtml::textField($pageObj->pageVar,$page,array('class' => 'bh-c-text1-yy'));
		echo '页';
		echo CHtml::submitButton('确定',array('class' => 'bh-c-queding1-yy'));
		$this->endWidget();
	}
	
	/**
	 * Creates a page button.
	 * You may override this method to customize the page buttons.
	 * @param string $label the text label for the button
	 * @param integer $page the page number
	 * @param string $class the CSS class for the page button. This could be 'page', 'first', 'last', 'next' or 'previous'.
	 * @param boolean $hidden whether this page button is visible
	 * @param boolean $selected whether this page button is selected
	 * @return string the generated button
	 */
	protected function createPageButton($label,$page,$class,$hidden,$selected)
	{
		$class .= ' ';
		if($selected) {
			$class .= 'bh-on ';
		}
		
		return CHtml::link($label,$this->createPageUrl($page), array('class'=>$class));

	}
}

?>